package com.superhelper.common.utils;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class JsonObjectUtil {

    public static String getListStr(Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof List) {
            List<?> list = (List<?>) obj;
            if (list.isEmpty()) {
                return null;
            }
            return StringUtils.join((List<?>) obj, ",");
        }
        return obj.toString();
    }

    public static Long getLong(Object obj) {
        if (obj == null) {
            return 0L;
        }
        if (obj instanceof Integer) {
            return ((Integer) obj).longValue();
        }
        return (Long) obj;
    }

    public static Integer getInt(Object obj) {
        if (obj == null) {
            return 0;
        }
        if (obj instanceof Long) {
            return ((Long) obj).intValue();
        }
        if (obj instanceof Double) {
            return ((Double) obj).intValue();
        }
        return (Integer) obj;
    }

    public static Double getDouble(Object obj) {
        if (obj == null) {
            return 0D;
        }
        if (obj instanceof Double) {
            return (Double) obj;
        } else if (obj instanceof Integer) {
            return BigDecimal.valueOf((Integer) obj).doubleValue();
        }
        return 0d;
    }

    public static Boolean getBoolean(Object obj) {
        if (obj == null) {
            return Boolean.FALSE;
        }
        if (obj instanceof Boolean) {
            return (Boolean) obj;
        }
        if (obj instanceof String) {
            return Boolean.valueOf(obj.toString());
        }
        return Boolean.FALSE;
    }
}
